<?php

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller
{
    public $userapp = [];
    public $auth;
    public $siteid;

    public $group = [
        'guest'     => '访客',
        'designer'  => '设计',
        'editor'    => '编辑',
        'seller'    => '销售',
        'responder' => '客服',
        'boss'      => '老板',
    ];
    public $defaultFolder = [
        '/asset',
        '/public',
        '/zip',
        '/page',
        '/post',
        '/theme',
        '/theme/default',
        '/theme/default/static',
        '/theme/default/static/css',
        '/theme/default/static/js',
        '/theme/default/static/img',
        '/theme/default/static/fonts',
        '/theme/default/lang',
        '/theme/default/template',
    ];

    public function initialize()
    {
        $this->tag->setTitle("igoji");

        $this->assets->addCss("/static/common/css/bootoast.css");
        $this->assets->addJs("/static/common/js/bootoast.js");
        $this->assets->addJs("/static/common/js/common.js");

        $this->view->auth = null;
        if ($this->cookies->has('auth')) {
            $this->auth       = $auth       = unserialize($this->cookies->get('auth'));
            $auth['group']    = $this->group[$auth['group']];
            $this->view->auth = $auth;

            $userapp = $this->redis->get('uid:' . $auth['id'] . ':app');
            if ($userapp) {
                $userapp = unserialize($userapp);
                foreach ($userapp as $key => $value) {
                    $this->userapp[] = $value['identifier'];
                }
            }
            $file               = file_get_contents(BASE_PATH . '/_app/config.json');
            $app                = json_decode($file, true);
            $this->view->navbar = $navbar = [];
            if ($this->userapp) {
                foreach ($this->userapp as $key => $value) {
                    isset($app[$value]['navbar']) && $navbar[$value] = $app[$value]['navbar'];
                }
                $this->view->navbar = $navbar;
            }
        }

        //查询站点信息
        $this->siteid = $this->dispatcher->getParam('siteid');
        if ($this->siteid) {
            $siteRedisKey = 'site:' . $this->siteid;
            if ($this->redis->get($siteRedisKey)) {
                $currentSite = unserialize($this->redis->get($siteRedisKey));
            } else {
                $row = MsSite::findFirst(
                    [
                        "conditions" => "md5 = ?1",
                        "bind"       => [
                            1 => $this->siteid,
                        ],
                    ]
                );
                if ($row) {
                    $row = $row->toArray();
                    $this->redis->set($siteRedisKey, serialize($row));
                    $currentSite = $row;
                } else {
                    echo '站点不存在，<a href="/">回到首页</a>';die;
                }
            }
            $this->view->currentSite = [$this->siteid => $currentSite];
            $this->view->siteid      = $this->siteid;
        }
    }
}
